import com.alibaba.fastjson.JSON;
import sql.query.*;
import sql.lexer.SQLLexer;
import sql.lexer.Token;
import sql.parser.*;

import java.util.List;

public class Main {

    private static void insertTest() {
        String sql = "insert into table1 (id,name,addr) values (100,陈大侠1,ds),(101,陈大侠2,ds),(1041,陈大侠3,ds)";
        SQLLexer sqlLexer = new SQLLexer(sql);
        InsertParser insertParser = new InsertParser(sqlLexer);
        InsertSyntax insertSyntax = insertParser.parse();

        ObjectInsert objectInsert = new ObjectInsert(new JsonManager(), insertSyntax);
        boolean ok = objectInsert.save();
        if(ok) {
            System.out.println("保存成功");
        } else {
            System.out.println("保存失败");
        }
    }



    private static void lexerTest() {
        String sql = "SELECT users.id, name FROM users WHERE age >= 18.5 AND name != 'John''s' OR email = 'test@example.com' -- This is a comment\nORDER BY id ASC LIMIT 10;";
        sql = "select * from table1 where zone='戴村2' and name = '陈大侠3' order by id asc, num asc limit 5";
        // sql = "select func(id,name,-123.22) as funcName,id,name, addr, (select (select city from table3 where zone = table2.zone) as city from table2 where addr = table1.addr and id >= 100) as city from table1 where id in (select id from table1 where addr = ds)";
        sql =  "SELECT id, -price AS cost, +10.5 AS bonus FROM products \n" +
                "WHERE price <= -100.25 OR discount > 0.15 \n" +
                "AND name != \"Special Item\" -- Negative price test\n" +
                "ORDER BY cost DESC, xxx asc LIMIT 5;";

        SQLLexer lexer = new SQLLexer(sql);
        List<Token> tokens = lexer.getAllTokens();

        for (Token token : tokens) {
            System.out.println(token);
        }
    }

    private static void selectTest() {
        String sql = " select (select * from test) as t, * from table1 where a = 1 and x = (select id from test where a = (select * from test where a = 1))";
        sql = "select func(id,name,123) as funcName,id,name, addr, (select (select city from table3 where zone = table2.zone) as city from table2 where addr = table1.addr and id >= 100) as city from table1 where id in (select id from table1 where addr = ds)";
        //sql = "select * from table2 where zone in (select zone from table3 where city = table2.city)";
        sql = "select * from table1 where name in (123,'陈大侠') or name = 'geluping' order by name desc limit 10";

       // sql = "select * from table4 where DEP_DELAY < 0 order by DEP_DELAY desc limit 10";
        SQLLexer lexer = new SQLLexer(sql);
        SelectParser selectParser = new SelectParser(lexer);
        SyntaxNode sqlNode = selectParser.parse();

        Transformer transformer = new Transformer(new JsonManager());
        ObjectQuery objectQuery = transformer.transformObjectQuery(sqlNode);
        List result = objectQuery.execList(null);
        System.out.println(JSON.toJSONString(result, true));
    }

    private static void updateTest() {
        String sql = "update table1 set zone = (select zone from table2 where addr = table1.addr) , name = 陈大侠1111 where addr = (select addr from table2 where id = 1)";
        sql = "update table1 set name = '123' where zone = '戴村'";
        SQLLexer sqlLexer = new SQLLexer(sql);
        UpdateParser updateParser = new UpdateParser(sqlLexer);
        SyntaxNode syntaxNode = updateParser.updateParse();
        //System.out.println(JSON.toJSONString(syntaxNode, true));
        ObjectUpdate objectUpdate = new ObjectUpdate(new JsonManager(), syntaxNode);
        objectUpdate.update();
    }


    private static void deleteTest() {
        String sql = "delete table1 where zone = '戴村'";
        SQLLexer sqlLexer = new SQLLexer(sql);
        DeleteParser updateParser = new DeleteParser(sqlLexer);
        SyntaxNode syntaxNode = updateParser.deleteParse();
        ObjectDelete objectUpdate = new ObjectDelete(new JsonManager(), syntaxNode);
        objectUpdate.delete();
    }

    public static void main(String[] args) {
       //lexerTest();
        //selectTest();
        //insertTest();
        // updateTest();
        deleteTest();
        System.out.println("============操作完成====================" );
    }
}
